//
//  VisitorLogin.swift
//  Weibo
//
//  Created by 明丽 on 16/1/11.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import SnapKit

//代理方法,必须遵守 NSObjectProtocol
@objc protocol visitorLoginViewDelegate : NSObjectProtocol{
//代理方法
    //注册
    func  userWillRegister()
//登录
    func userWillLogin()



}



class VisitorLogin: UIView {
    
    //代理属性,用弱指针修饰,放置循环引用
    weak var visitorViewDelegate: visitorLoginViewDelegate?
    @objc private func loginBtnDidClick() {
    
        visitorViewDelegate?.userWillLogin()
    
    }
    @objc private func registerBtnDidClick() {
        visitorViewDelegate?.userWillRegister()

    }
    func  setupUIWithInfo (stringName : String? , titleName : String){
        if let image = stringName{
            circleIcon.image=UIImage(named: image)
            //隐藏大房子
            largehouse.hidden = true
            bringSubviewToFront(circleIcon)
            
            
        }else{
            sterAnimation()
            
        }
        tipLable.text=titleName
        
    }
    //动画效果
    func sterAnimation() {
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        base.duration = 10
        //设置动画完成/ 页面失去活跃状态  不移除图层
        base.removedOnCompletion = false
        circleIcon.layer.addAnimation(base, forKey: nil)
        
        
        
        
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setupUI()
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加到view上并设置约束
    func setupUI(){
        addSubview(circleIcon)
        addSubview(backView)
        
        addSubview(largehouse)
        addSubview(tipLable)
        addSubview(leftButton)
        addSubview(rightButton)
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        largehouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(20)
            make.width.equalTo(225)
            
        }
        leftButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_bottom).offset(20)
            make.width.equalTo(100)
            
        }
        rightButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        
        //        backView.snp_makeConstraints { (make) -> Void in
        //            make.top.left.right.equalTo(self)
        //            make.bottom.equalTo(rightButton.snp_bottom)
        
        // }
        backView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(rightButton.snp_bottom)
            
        }
        //灰度颜色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加按钮的监听事件
        leftButton.addTarget(self, action:  "loginBtnDidClick", forControlEvents: .TouchUpInside)
        rightButton.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        
    }
    
    //遮罩圈
    lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    lazy var largehouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    lazy var tipLable : UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(16)
        l.text = "请登录,登录后别人会评论你的微薄,进行交友聊天,分享心情,更多有趣的事情等待你来探索"
        l.numberOfLines = 0
        l.textColor =  UIColor.darkGrayColor()
        l.textAlignment = .Center
        l.sizeToFit()
        
        return l
    }()
    //左边按钮
//    lazy var leftButton : UIButton = {
//        let btn = UIButton()
//        
//        //设置按钮的文字
//        btn.setTitle("登录", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//        
//    }()
    lazy var leftButton: UIButton = UIButton(title: "登录", fontSize: 16, color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
    
    
    //左边按钮
//    lazy var rightButton : UIButton = {
//        let btn = UIButton()
//        //设置按钮的文字
//        btn.setTitle("注册", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//        
//    }()
//    
    lazy var rightButton: UIButton = UIButton(title: "注册", fontSize: 16, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
}

